package com.fram.controller.admin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.admin.distributor.request.DistributorAdminUserAddRequest;
import com.fram.bean.api.project.request.CategoryRequest;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseAdminController;
import com.fram.entity.DistributorFarm;
import com.fram.entity.DistributorProjectCategory;
import com.fram.service.admin.DistributorService;

/**后台-分销商管理（超级管理可操控）**/
@RestController
@RequestMapping("/admin/{version}/auth/distributors")
public class DistribuController extends BaseAdminController {

	/** 日志 **/
	private static Logger LOG = LoggerFactory.getLogger(DistribuController.class);

	@Autowired
	private DistributorService distributorService;

	/** 列表（也包含下拉） **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	private ResponseMsg list(@RequestBody PageBean<DistributorFarm> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (request.getParam() == null) {
				request.setParam(new DistributorFarm());
			}
			request.getParam().setId(getDistributorId());
			msg.setData(distributorService.list(request));
		} catch (ProException e) {
			LOG.error("-后台-分销商列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台-分销商列表-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 详情 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/details", method = RequestMethod.GET)
	private ResponseMsg details(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(distributorService.details(id));
		} catch (ProException e) {
			LOG.error("-后台-分销商列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台-分销商列表-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 新增 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	private ResponseMsg add(@RequestBody DistributorFarm request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			distributorService.add(request);
		} catch (ProException e) {
			LOG.error("-后台-分销商列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台-分销商列表-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 修改 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/update")
	private ResponseMsg update(@RequestBody DistributorFarm request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			distributorService.update(request);
		} catch (ProException e) {
			LOG.error("-后台-分销商列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台-分销商列表-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

//	@ApiVersion(1.0)
//	@RequestMapping("/category/add")
//	private ResponseMsg categoryadd(Long distributorId, @RequestBody List<Long> categoryIds) {
//		ResponseMsg msg = ResponseMsg.returnBeanBuild();
//		try {
//			distributorService.insertProjectCategory(distributorId, categoryIds);
//		} catch (ProException e) {
//			LOG.error("-后台-分销商-分销商增加售卖的类型->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
//			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
//		} catch (Exception e) {
//			LOG.error("-后台-分销商-分销商增加售卖的类型-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
//			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
//		}
//		return msg;
//	}

	/** 分销商 -分类管理 传入id 展示 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/category/list", method = RequestMethod.POST)
	private ResponseMsg listCategory(@RequestBody CategoryRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(distributorService.ListByFarmId(request));
		} catch (ProException e) {
			LOG.error("-分类展示->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-分类展示-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/add/category", method = RequestMethod.POST)
	private ResponseMsg addCategory(@RequestBody DistributorProjectCategory request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setDistributorId(getDistributorId());
			distributorService.addProjectCategroy(request);
		} catch (ProException e) {
			LOG.error("-分销商增加 一二类别->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-分销商增加 一二类别-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	/** 分销商管理员帐号添加 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/userlogin/add")
	private ResponseMsg register(@RequestBody DistributorAdminUserAddRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			distributorService.addDistributorAdminUser(request);
		} catch (ProAffairException e) {
			LOG.error("-分销商-帐号添加 出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-分销商-帐号添加 异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("category/delete")
	private ResponseMsg categoryDelete(Long id,Long distributorId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(distributorService.deleteProjectCategroy(id,distributorId));
		} catch (ProException e) {
			LOG.error("-删除类型出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-删除类型异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
